/**
 * Copyright 2009-2011 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.myjung.model;

import java.util.ArrayList;
import java.util.List;

import net.myjung.Myjung;
import net.myjung.model.annotation.MdlAttribute;
import net.myjung.model.util.IdGenerator;

/**
 * @author Steve M. Jung
 */
@SuppressWarnings("serial")
public class Filters extends BaseObject {
	public static final String OPERATOR_AND = "and";
	public static final String OPERATOR_OR = "or";
	@MdlAttribute
	private String operator = OPERATOR_AND;
	private List<Filter> filter;
	private List<Filters> filters;
	public String getOperator() {
		return operator;
	}
	public void setOperator(String operator) {
		this.operator = operator;
	}
	public List<Filter> getFilter() {
		return filter;
	}
	public Filter getFilter(String name) {
		if (Myjung.isEmpty(filter))
			return null;
		for (Filter f : filter) {
			if (name.equals(f.getLeftOperand()))
				return f;
		}
		return null;
	}
	public void setFilter(List<Filter> filter) {
		this.filter = filter;
	}
	public void setFilter(String name, Object value) {
		setFilter(name, "=", null, value);
	}
	public void setFilter(String name, String operator, Object value) {
		setFilter(name, operator, null, value);
	}
	public void setFilter(String name, String operator, String type, Object value) {
		Filter filter = getFilter(name);
		if (filter == null) {
			addFilter(name, operator, type, value);
			return;
		}
		filter.setOperator(operator);
		filter.setRightOperandType(type);
		filter.setRightOperand(toValueString(value));
	}
	public Filter addFilter(Filter filter) {
		if (this.filter == null)
			this.filter = new ArrayList<Filter>();
		this.filter.add(filter);
		return filter;
	}
	public Filter addFilter(String name, Object value) {
		return addFilter(name, "=", null, value);
	}
	public Filter addFilter(String name, String operator, Object value) {
		return addFilter(name, operator, null, value);
	}
	public Filter addFilter(String name, String operator, String type, Object value) {
		return addFilter(new Filter(name, operator, type, toValueString(value)));
	}
	public Filter removeFilter(String name) {
		Filter filter = getFilter(name);
		if (filter != null)
			this.filter.remove(filter);
		return filter;
	}
	private static String toValueString(Object value) {
		if (value instanceof BaseObject)
			value = IdGenerator.getId((BaseObject) value);
		return value == null ? null : value.toString();
	}
	public String getFilterValue(String name) {
		Filter filter = getFilter(name);
		if (filter == null)
			return null;
		return Myjung.isEmpty(filter.getRightOperand()) ? null : filter.getRightOperand().get(0);
	}
	public List<Filters> getFilters() {
		return filters;
	}
	public void setFilters(List<Filters> filters) {
		this.filters = filters;
	}
	public Filters addFilters(Filters filters) {
		if (this.filters == null)
			this.filters = new ArrayList<Filters>();
		this.filters.add(filters);
		return filters;
	}
	@Override
	public Object get(String name) {
		Filter f = getFilter(name);
		if (f == null)
			return super.get(name);
		if (f.getRightOperand().size() == 1)
			return f.getRightOperand().get(0);
		return f.getRightOperand();
	}
	@Override
	public void set(String name, Object value) {
		if (field(name) == null) {
			setFilter(name, value);
			return;
		}
		super.set(name, value);
	}
}